<?php
/**
 * Created by PhpStorm.
 * User: Raytine
 * Date: 2019/3/24
 * Time: 21:59
 */

namespace App\Repository;


use App\Events\AddCustomerEvent;
use App\model\Area;
use App\model\baiduForm;
use App\model\Coustomer;
use App\model\Industry;
use App\model\SendNoticeUser;
use App\model\tengxunForm;
use App\User;

class CustomerRepository
{
    public function addCustomer()
    {
        tengxunForm::chunk(1, function ($tengxun) {
            foreach ($tengxun as $value) {
                $this->newcustomer($value);
            }
        });
    }

    public function addBaiduCustomer()
    {
        baiduForm::chunk(1, function ($tengxun) {
            foreach ($tengxun as $value) {
                $this->newcustomerBaidu($value);
            }
        });
    }

    public function newcustomerBaidu($value, $project_id = '')
    {

        $this->customer(
            collect($value->data['data'])->firstWhere('name', '=', '姓名')['value'],
            collect($value->data['data'])->firstWhere('name', '=', '电话')['value'],
            collect($value->data['data'])->firstWhere('name', '=', '性别')['value'],
            collect($value->data['data'])->firstWhere('name', '=', '行业')['value'],
            collect($value->data['data'])->firstWhere('name', '=', '需求面积')['value'],
            $project_id,
            5
        );

    }

    public function newcustomer($value, $project_id = '')
    {
        $username = $value->data['leads_name'] ?? '未命名';
        $mobile = $value->data['mobile'] ?? '';
        $area = $value->data['tel_location'] ?? '';
        $this->customer(
            $username,
            $mobile,
            '',
            '',
            $area,
            $project_id,
            3
        );
    }

    protected function customer($name, $mobile, $sex, $industry = '', $area = '', $project_id = '', $source_id)
    {

        /* $customer=Coustomer::where('mobile',$mobile)->where('project_id','=',$project_id)->first();

         if ($customer){

             return true;
         }*/
        $customer = new Coustomer();
        if ($industry) {
            $industry = Industry::firstOrCreate(['name' => $industry]);
            $customer->industry_id = $industry->industry_id;
        }
        if ($area) {
            $area = Area::firstOrCreate(['name' => $area]);
            $customer->area_id = $area->id;
        }
        if ($project_id) {
            $customer->project_id = $project_id;
        }
        $customer->source_id = $source_id;
        $customer->name = $name;
        $customer->mobile = $mobile;
        $customer->sex = $sex;
        $customer->save();

        //发送通知
        $this->sendXiaoshou($customer);

    }

    public function sendXiaoshou(Coustomer $coustomer)
    {
        //发送通知
        $wechat = new WechatRepository();
        if ($coustomer->project_id) {
            //查找此项目的项目经理 发送通知
            $sendNotice = SendNoticeUser::find(1);
            $sendUser = $sendNotice->role;
            if (!empty($sendUser)) {
                $user = User::join('model_has_roles', 'model_has_roles.model_id', '=', 'users.id')
                    ->whereIn('model_has_roles.role_id', $sendUser)
                    ->where(\DB::raw("find_in_set($coustomer->project_id,project_id)"), '>', 0)
                    ->get()->unique('id')->values();;
                foreach ($user as $v) {
                    if (in_array($coustomer->project_id, $v->no_project_id)) {
                        continue;
                    }
                    \Log::info('增加用户提醒', $v->toArray());
                    event(new AddCustomerEvent($v, $coustomer));

                }
            }
        }
    }
}
